import { reqcate, reqcategoods } from '../../request/api';

//状态
const initState = {
    getcate: [],
    getcategoods: [],
    n: 0,
    fid: 1,
    name: "时令水果"
}

//actionType
const TYPES = {
    CATE_CHANGE_GETCATE: 'CATE_CHANGE_GETCATE',
    CATE_CHANGE_GETCATEGOODS: 'CATE_CHANGE_GETCATEGOODS',
    CATE_CHANGE_N: 'CATE_CHANGE_N',
    CATE_CHANGE_FID: 'CATE_CHANGE_FID',
    CATE_CHANGE_NAME: 'CATE_CHANGE_NAME',
}

//修改state reducer 
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.CATE_CHANGE_GETCATE:
            return {
                ...state,
                getcate: action.getcate
            }
        case TYPES.CATE_CHANGE_GETCATEGOODS:
            return {
                ...state,
                getcategoods: action.getcategoods
            }
        case TYPES.CATE_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
        case TYPES.CATE_CHANGE_FID:
            return {
                ...state,
                fid: action.fid
            }
        case TYPES.CATE_CHANGE_NAME:
            return {
                ...state,
                name: action.name
            }
        default:
            return state;
    }
}

//做逻辑和异步的acitons actionCreators
export const actions = {
    changeGetcate: (getcate) => ({ type: TYPES.CATE_CHANGE_GETCATE, getcate }),
    requestCate: () => {
        return (dispatch, getState) => {
            // 判断状态层是否有数据，没有数据才请求
            let cate = getCate(getState())
            if (cate.length > 0) {
                return;
            }
            reqcate().then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeGetcate(res.data.list))
                }
            })
        }
    },
    changeGetcategoods: getcategoods => ({ type: TYPES.CATE_CHANGE_GETCATEGOODS, getcategoods }),
    requestCategoods: () => {
        return (dispatch, getState) => {
            let fid = getFid(getState());
            reqcategoods({ fid }).then(res => {
                if (res.data.code == 200) {
                    dispatch(actions.changeGetcategoods(res.data.list))
                }
            })
        }
    },
    changeFid: fid => ({ type: TYPES.CATE_CHANGE_FID, fid }),
    chgN: n => ({ type: TYPES.CATE_CHANGE_N, n }),
    changeName: name => ({ type: TYPES.CATE_CHANGE_NAME, name }),
    changeN: (index, id, name) => {
        return (dispatch, getState) => {
            dispatch(actions.changeFid(id));
            dispatch(actions.chgN(index));
            dispatch(actions.changeName(name));
            let fid = getFid(getState());
            reqcategoods({ fid }).then(res => {
                dispatch(actions.changeGetcategoods(res.data.list))
            })
        }
    }
}

//导出数据 Selectors 
export const getCate = state => state.cate.getcate;
export const getCategoods = state => state.cate.getcategoods;
export const getFid = state => state.cate.fid;
export const getN = state => state.cate.n;
//导出reducer 
export default reducer;